package sculove.spring.zzz.comCd.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import sculove.spring.framework.common.AbstractService;
import sculove.spring.zzz.comCd.dao.ComCdDAO;
import sculove.spring.zzz.comCd.model.ComCdDTO;

@Service("zzz.comCdService")
public class ComCdServiceImpl extends AbstractService implements ComCdService {

	@Resource(name="zzz.comCdDAO")
    private ComCdDAO comCdDao;

    public ComCdDAO getComCdDao(){
        return comCdDao;
    }

    public void setComCdDao(ComCdDAO comCdDao){
        this.comCdDao = comCdDao;
    }

    @Override
    public List<ComCdDTO> inqComCdLst() throws Exception{
        List<ComCdDTO> list = comCdDao.inqComCdLst();
        logger.debug("☆☆☆☆☆☆☆ 조회수 : " + list.size());
        return list;
    }

    @Override
    public HashMap<String, List<ComCdDTO>> inqComCd(HashMap<String, ComCdDTO> cdList) throws Exception{
        HashMap<String, List<ComCdDTO>> lstMap = new HashMap<String, List<ComCdDTO>>();
        
        Iterator<String> iter =cdList.keySet().iterator();
        String key = null;
        while(iter.hasNext()) {
            key = iter.next();
            List<ComCdDTO> lstCd = ComCdManager.get(key);    
            
            // DB에서 조회
            if(lstCd == null){
                //자식공통코드 조회
                if(ComCdManager.isChildKey(key)){
                    lstCd = comCdDao.inqHgrnkComCd(cdList.get(key));
                }else{ //공통코드 조회    
                    lstCd = comCdDao.inqComCd(cdList.get(key));
                }
            }
            
            // 조회된 결과가 0건 이상일 경우
            if(lstCd.size() != 0){
                ComCdManager.put(key, lstCd); // 메모리 캐쉬에 저장
                lstMap.put(key, lstCd); // 반환값에 저장
            }            
        }
        return lstMap;
    }
}
